'use strict';

const Controller = require('egg').Controller;

class PdfController extends Controller {
    async index() {
        const ctx = this.ctx;
		const rule = {
            date: 'string',
            url:'string',
		}
		const errors = this.app.validator.validate(rule,ctx.request.body);
		if(errors){
			ctx.body = ctx.helper.fail('参数不对',errors);
			return;
		}
        const sendDate = ctx.request.body;
        const res = await ctx.service.pdf.create(sendDate);
		ctx.body = res;
	}
	
	async index2() {
        const ctx = this.ctx;
		const rule = {
			date: 'string',
			url:'string',
			part:'string'
		}
		const errors = this.app.validator.validate(rule,ctx.request.body);
		if(errors){
			ctx.body = ctx.helper.fail('参数不对',errors);
			return;
		}
        const sendDate = ctx.request.body;
        const res = await ctx.service.pdf2.create(sendDate);
		ctx.body = res;
	}
	
	async merge(){
		const ctx = this.ctx;
		const rule = {
			date: 'string',
			source: 'string'
		}
		const errors = this.app.validator.validate(rule,ctx.request.body);
		if(errors){
			ctx.body = ctx.helper.fail('参数不对',errors);
			return;
		}
        const sendDate = ctx.request.body;
        const res = await ctx.service.merge.create(sendDate);
		ctx.body = res;
	}

	// 根据url生成对应的pdf
	async url(){
		const ctx = this.ctx;
		const rule = {
			url:'string',
			size:'string?',
			marginLeft:'string?',
			marginTop:'string?',
			marginBottom:'string?',
			marginRight:'string?'
		}
		const errors = this.app.validator.validate(rule,ctx.request.body);
		if(errors){
			ctx.body = ctx.helper.fail('参数不对',errors);
			return;
		}
        const sendDate = ctx.request.body;
        const res = await ctx.service.url.create(sendDate);
		ctx.body = res;
	}
        
}

module.exports = PdfController;